<template>
  <div>
    <div class="licc-goods-list-info-top">
      <el-form :model="filterGoodsList" ref="filterGoodsList"  label-width="80px">
        <el-row>
          <el-col :span="5">
            <el-form-item
              label="商品货号"
              prop="goodSn"
              :rules="[{trigger: 'blur', validator: validateGoodsSnRule}]">
              <el-input v-model="filterGoodsList.goodSn" placeholder="请输入商品货号"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-form-item
              label="商品名称"
              prop="goodsName"
              :rules="[{trigger: 'blur', validator: validateGoodsNameRule}]">
              <el-input v-model="filterGoodsList.goodsName" placeholder="商品名称"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="5">
            <el-form-item label="库存状态">
              <el-select v-model="warnNumber" clearable placeholder="请选择">
                <el-option
                  v-for="item in warnNumbers"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label=添加时间>
              <el-date-picker
                v-model="datePickerValue"
                value-format="yyyy-MM-dd HH:mm:SS"
                :picker-options="pickerOptions"
                type="daterange"
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="4" :offset="7">
            <el-button type="info" @click="restGoodsInfosButton">重 置</el-button>
            <el-button type="primary" @click="selectGoodsInfosButton">筛 选</el-button>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <div>
      <el-table
        stripe
        border
        :data="goodsList"
        :default-sort = "{prop: 'createDate', order: 'descending'}">
        <el-table-column type="index" width="50"></el-table-column>
        <el-table-column prop="goodsSn" label="商品货号" width="180" sortable></el-table-column>
        <el-table-column prop="goodsName" label="商品名称" width="180"></el-table-column>
        <el-table-column prop="shopPrice" label="销售价" width="80"></el-table-column>
        <el-table-column prop="goodsNumber" label="库存" width="80"></el-table-column>
        <el-table-column prop="warnNumber" label="库存状态" width="100">
          <template slot-scope="scope">
            <el-tag :type="scope.row.warnNumber==1?'success':scope.row.goodsNumber==0?'warning':'danger'">
              <!--库存为1时 库存充足， 为2 时库存预警， 在为2时，总库存为0时缺货-->
              {{scope.row.warnNumber==1?'库存充足':scope.row.goodsNumber==0?'库存预警':'缺货中'}}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="integral" label="总销量" width="100" sortable>
          <template slot-scope="scope">
            <strong style="color: #F56C6C; font-size: 16px">x {{scope.row.integral}}</strong>
          </template>
        </el-table-column>
        <el-table-column prop="createDate" label="添加时间" width="160" sortable></el-table-column>
        <el-table-column prop="goodsBrief" label="简短描述"></el-table-column>
        <el-table-column fixed="right" width="320" label="操作">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="warning"
              @click="handleProductEdit(scope.$index, scope.row)">库存</el-button>
            <el-button
              size="mini"
              type="primary"
              @click="handleEdit(scope.$index, scope.row)">详情</el-button>
            <el-button
              size="mini"
              type="danger"
              @click="goodsGoOnline(scope.$index, scope.row)">重新上线</el-button>
            <el-button
              size="mini"
              type="danger"
              @click="goodsDelete(scope.$index, scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="block">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[10, 20, 35, 50]"
        :page-size="pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="goodsTotal">
      </el-pagination>
    </div>
    <div>
      <el-dialog
        title="商品详情"
        :visible.sync="dialogGoodsInfoVisible"
        width="70%" style="text-align: left">
        <span>
          <goods-info v-if="dialogGoodsInfoVisible" :message="goodsInfoCheck"></goods-info>
        </span>
        <span slot="footer" class="dialog-footer">
          <el-button type="primary" @click="dialogGoodsInfoVisible = false">返 回</el-button>
        </span>
      </el-dialog>
    </div>
    <div>
      <el-dialog
        title="商品库存详情"
        :visible.sync="dialogGoodsProductVisible"
        width="70%" style="text-align: left">
        <span>
          <shop-products v-if="dialogGoodsProductVisible" :message="goodsProductCheck" @productInfoBackClick="dialogGoodsProductVisible = false"  @productInfoSubmit="productInfoSubmitClick"></shop-products>
        </span>
      </el-dialog>
    </div>
  </div>
</template>

<script>

  import goodsApi from '@/api/backstage/goods'
  import forSaleApi from '@/api/backstage/forSale'
  import GoodsInfo from '../components/goodsInfo'
  import ShopProducts from '../components/shopProducts'
  export default {
    name: 'Goods_listInfo',
    components: {
      GoodsInfo,
      ShopProducts
    },
    data() {
      return {
        filterGoodsList:{
          goodSn:'',
          goodsName:''
        },
        datePickerValue:'',
        warnNumbers:[{
          value: '1',
          label: '库存充足'
        }, {
          value: '2',
          label: '库存预警'
        }, {
          value: '3',
          label: '缺货中'
        }],
        warnNumber:null,
        goodsList: [],
        pickerOptions: {
          disabledDate(time) {
            return time.getTime() > Date.now() - 8.64e6
          }
        },
        params2: {
          status: [100],
        },
        currentPage: 1,
        pagesize:20,
        goodsTotal:0,
        /** 商品详情显示 **/
        dialogGoodsInfoVisible: false,
        goodsInfoCheck:{},
        /** 商品库存详情 **/
        dialogGoodsProductVisible: false,
        goodsProductCheck:{},
      }
    },
    created() {
      this.initData()
    },
    methods: {
      initData(){
        this.goodsTotal = 0;
        this.goodsList = [];
        goodsApi.getGoodsDataList(null,this.params2, this.currentPage, this.pagesize).then(res => {
          this.goodsList = res.data.records;
          this.goodsTotal = res.data.total;
        })
      },
      validateGoodsSnRule(rule, value, callback){
        if (typeof (value) == 'undefined'||value==null||value===''){
          callback()
        } else if (value.length < 3||value.length>20){
          callback(new Error('只能由3-20位字符组成'));
        } else {
          callback();
        }
      },
      validateGoodsNameRule(rule, value, callback){
        if (typeof (value) == 'undefined'||value==null||value===''){
          callback()
        } else if (value.length < 3||value.length>50){
          callback(new Error('只能由3-50位字符组成'));
        } else {
          callback();
        }
      },
      /** 分页---开始 **/
      handleSizeChange(val) {
        this.pagesize = val;
        this.initData();
        console.log(`每页 ${val} 条`);
      },
      handleCurrentChange(val) {
        this.currentPage = val;
        this.initData();
        console.log(`当前页: ${val}`);
      },
      /** 分页---结束 **/
      /** 筛选重置 **/
      restGoodsInfosButton(){
        this.datePickerValue = null;
        this.filterGoodsList.goodSn = '';
        this.filterGoodsList.goodsName = '';
        this.$nextTick(() => this.$refs['filterGoodsList'].clearValidate())
        this.currentPage = 1;
        this.params2 = {
          status: [100],
        }
        this.initData();
      },
      /** 筛选事件 **/
      selectGoodsInfosButton(){
        this.params2 = {
          status: [100],
        }
        this.$refs['filterGoodsList'].validate(valid => {
          if (valid){
            this.params2 = {
              status: [100],
              goodsSn: this.filterGoodsList.goodSn,
              goodsName: this.filterGoodsList.goodsName,
              warnNumber: this.filterGoodsList.warnNumber,
            }
            if (this.datePickerValue!=null&&this.datePickerValue.length==2){
              this.params2.startTime = this.datePickerValue[0];
              this.params2.endTime = this.datePickerValue[1];
            }
            this.currentPage = 1;
            this.initData();
          } else {
            this.$notify({title: '提示', message: '输入的信息不合规', type: 'warning'});
          }
        });
      },
      /** 库存详情 **/
      handleProductEdit(index,row){
        this.goodsProductCheck = row;
        this.dialogGoodsProductVisible = true;
      },
      /** 库存修改成功后 **/
      productInfoSubmitClick(){
        this.initData();
        this.dialogGoodsProductVisible = false;
      },
      /** 详情页面 **/
      handleEdit(index,row){
        this.goodsInfoCheck = row;
        this.dialogGoodsInfoVisible = true;
      },
      goodsGoOnline(index,row) {
        this.$confirm('此操作将会使商品货号“'+row.goodsSn+'”的商品重新上线, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          let params = {
            id: row.id,
            reviewStatus: 10
          }
          forSaleApi.updateGoods(params).then(res => {
            this.initData();
            this.$notify.success({title: '成功', message: '商品成功上线'});
          }).catch(() => {
            this.$notify.warning({title: '失败', message: '商品上线失败，请稍后再试！'});
          })
        }).catch(() => {
          this.$notify.info({title: '取消', message: '已取消商品上线'});
        });
      },
      goodsDelete(index,row) {
        this.$confirm('此操作将会永久删除商品货号“'+row.goodsSn+'”的商品, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          let params = {
            id: row.id,
            reviewStatus: 120,
            hot:0,
            newGoosa:0
          }
          forSaleApi.updateGoods(params).then(res => {
            this.initData();
            this.$notify.success({title: '成功', message: '商品成功删除'});
          }).catch(() => {
            this.$notify.warning({title: '失败', message: '商品删除失败，请稍后再试！'});
          })
        }).catch(() => {
          this.$notify.info({title: '取消', message: '已取消商品删除'});
        });
      },
    }
  }
</script>
<style>
  .licc-goods-list-info-top{
    width: 100%;
    min-width: 1100px;
  }
</style>
